<?php


// Inialize session
session_start();

// Check, if username session is NOT set then this page will jump to login page
if (!isset($_SESSION['username'])) {
        header('Location: index.php');
}


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
	<title>Cropping Plan</title>
<script src="jquery.js" language="javascript" type="text/javascript"></script>

    
    
<SCRIPT LANGUAGE="JavaScript" type="text/javascript">

function viewCrop(cropName, categoryName, cropArea, nextCategory){
    var data = "<table><tr><td>Crop:</td><td>" + cropName + "</td></tr>";
    data = data + "<tr><td>Category:</td><td>" + categoryName + "</td></tr>";
    data = data + "<tr><td>Appx. Area:</td><td>" + cropArea + " sq ft</td></tr>"
    data = data + "<tr><td>Successor:</td><td>" + nextCategory + "</td></tr></table>"


    $("#cropData").html(data);
}

function setModeFromJavascript(mode){
    document.jsap.setModeFromJavascript(mode);
}

function setCropFromJavascript(id){
    document.jsap.setCropFromJavascript(id);
}

function setCropFromJava(id){
    document.getElementById("cropselector").value = id;
}

function displayError(str){
    alert(str);
}

function newPlan(){
    var id = document.getElementById("blockselector").value;
    
    var planname = prompt("Please enter a name for the new plan.");
    
    var url = "./croppingPlan.php?blockid=" + id + "&planname=";
    
    if(planname != null){
        $.ajax({
            type: "POST",
            url: "saveCroppingPlan.php",
            data: "overwrite=0&new=1&blockid=" + id + "&name=" + planname,
            success: function(msg){
                if(msg.substring(0,15) == "Save succeeded!"){
                    window.location = url + msg.substring(15);
                }else{
                    alert( msg );
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
                alert("Could not create a new map. Could not connect to the server.");
            }
        });
    }
}

function save(){
    var s = document.jsap.serializePlan();
    var id = document.getElementById("blockselector").value;
    
    var planname = $("#planTitle").attr("planname");

    var blocktype = 0;
    if(document.getElementById('raised_radio').checked){
        blocktype = 1;
    }
    
    var url = "./croppingPlan.php?blockid=" + id + "&planname=";
    
    if(planname != null){
        $.ajax({
            type: "POST",
            url: "saveCroppingPlan.php",
            data: "overwrite=1&blockid=" + id + "&name=" + planname + "&blocktype=" + blocktype + "&plan=" + s,
            success: function(msg){
                if(msg.substring(0,15) == "Save succeeded!"){
                    alert("Save succeeded!");
                    window.location = url + msg.substring(15);
                }else{
                    alert( msg );
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
                alert("Save failed. Could not connect to the server.");
            }
        });
    }else{
        alert("Save Cancelled.");
    }
}

function saveAs(){
    var s = document.jsap.serializePlan();
    var id = document.getElementById("blockselector").value;
    
    var planname = prompt("Please enter a name for this plan.");
    
    var url = "./croppingPlan.php?blockid=" + id + "&planname=";
    
    var blocktype = 0;
    if(document.getElementById('raised_radio').checked){
        blocktype = 1;
    }
    
    if(planname != null){
        $.ajax({
            type: "POST",
            url: "saveCroppingPlan.php",
            data: "overwrite=0&blockid=" + id + "&name=" + planname + "&blocktype=" + blocktype + "&plan=" + s,
            success: function(msg){
                if(msg.substring(0,15) == "Save succeeded!"){
                    alert("Save succeeded!");
                    window.location = url + msg.substring(15);
                }else{
                    alert( msg );
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
                alert("Save failed. Could not connect to the server.");
            }
        });
    }else{
        alert("Save Cancelled.");
    }
}

function setBlock(id){
    var url = "./croppingPlan.php?blockid=" + id;
    window.location = url;
}

function setPlan(planname){
    var blockid = document.getElementById("blockselector").value;
    var url = "./croppingPlan.php?blockid=" + blockid + "&planname=" + planname;
    window.location = url;
}

</SCRIPT>

	
</head>

<body>
<?php 
include_once 'plan_func.php';
include_once 'functions.php';


function cmp($a, $b){
    if ($a['cropname'] == $b['cropname']) {
        return 0;
    }
    return ($a['cropname'] < $b['cropname']) ? -1 : 1;
}


$allBlocks = getAllBlocks();

/*categoryname;categoryid!...*/

$categoryNameString = null;
$allCategories = getAllCategories();

for($i = 0; $i<count($allCategories); $i++){
	$categoryNameString = $categoryNameString.$allCategories[$i]['categoryname'].';'.$allCategories[$i]['categoryid'].';'.$allCategories[$i]['successor'].'!';
}

/*cropname;cropid;catid!...*/

$cropNameString = null;
$allCrops = getAllCrops();
for($i = 0; $i<count($allCrops); $i++){
	$cropNameString = $cropNameString.$allCrops[$i]['cropname'].';'.$allCrops[$i]['cropid'].';'.$allCrops[$i]['categoryid'].'!';
}

$allCrops[] = array('cropid' => -2, 'cropname' => 'Cover Crop');
$allCrops[] = array('cropid' => -3, 'cropname' => 'Research');

uasort($allCrops, 'cmp');

$firstCrop = $allCrops[0]['cropid'];

if(isset($_GET['blockid']) && $_GET['blockid']){
    $firstBlock = null;
    foreach($allBlocks as $b){
        if($b['blockid'] == $_GET['blockid']){
            $firstBlock = $b;
            break;
        }
    }
    if($firstBlock == null){
        die("Non-existant block id requested: " . $_GET['blockid']);
    }

}else{
    $firstBlock = $allBlocks[0];
}

$allPlans = getPlansFromBlock($firstBlock['blockid']);

$firstPlan = null;
if(isset($_GET['planname']) && $_GET['planname']){
    foreach($allPlans as $p){
        if($p['name'] == $_GET['planname']){
            $firstPlan = $p;
            break;
        }
    }
}

if($firstPlan == $null){
    $firstPlan = $allPlans[0];
}


$planString = $firstPlan['planstr'];

?>

	<?php
			include 'headinfo.php';
	?>
	<hr />
	<!-- end #logo -->
	<div id="header">
		<?php
			$thispage = "Cropping Plan";
			require 'menu.php';
		?>
		<!-- end #menu -->
	</div>
	<!-- end #header -->
	<!-- end #header-wrapper -->
	<div id="page">
		<div id="content">
            <div class="post">
		  		<!-- Start of content-->
				<h2 class="title"><a href="#">Cropping Plan</a></h2>
				<p class="meta"></p>

				
                
                <br />
                <p>
                Block: 
                <select name="blockselector" id="blockselector" onchange="setBlock(this.value)">
                <?php foreach($allBlocks as $b): ?>
                    <?php if($b == $firstBlock): ?>
                        <option selected="selected" value="<?php echo $b['blockid']?>">
                    <?php else: ?>
                        <option value="<?php echo $b['blockid']?>">
                    <?php endif; ?>
                        <?php echo $b['blockname'] ?>
                        </option>
                <?php endforeach; ?>
                </select>
                </p>
 
                <p>

                Plan: 
                <select name="planselector" id="planselector" onchange="setPlan(this.value)">
                <?php foreach($allPlans as $p): ?>
                    <?php if($p == $firstPlan): ?>
                        <option selected="selected" value="<?php echo $p['name']?>">
                    <?php else: ?>
                        <option value="<?php echo $p['name']?>">
                    <?php endif; ?>
                        <?php echo $p['name'] ?>
                        </option>
                <?php endforeach; ?>
                </select>
                <input type="button" value="New Plan" onclick="newPlan()" />
                </p>
                
                
				
				<p>
				
                <input type="button" value="Save" onclick="save()" />
                <input type="button" value="Save As" onclick="saveAs()" />
                </p>
                
                
                <p>
				
                <?php if($firstPlan['blocktype'] != 1): ?>
                    <input type="radio" id="native_radio" name="blocktype" checked>Native Soil
                    <input type="radio" id="raised_radio" name="blocktype">Raised Bed<br />
                <?php else: ?>
                    <input type="radio" id="native_radio" name="blocktype">Native Soil
                    <input type="radio" id="raised_radio" name="blocktype" checked>Raised Bed<br />
                <?php endif; ?>
                
                </p>
				<br /> 
                <p style="text-align: center"></p>
                
                <p class="meta"></p>
                
                
                <span id="planTitle" planname="<?php echo $firstPlan['name'] ?>"><h2><center>
                    <?php echo $firstBlock['blockname']."(".$firstPlan['name'].")" ?>
                </center></h2></span>
				<br />
				<p style="text-align: center">
				
				<input type="radio" name="mode" onchange="setModeFromJavascript('modify')">Modify Mode
                <input type="radio" name="mode" onchange="setModeFromJavascript('view')" checked>View Mode<br />
                
                </p>
                
                
				<p>
                Select Crop: <select name="cropselector" id="cropselector" onchange="setCropFromJavascript(this.value)">
                <?php foreach($allCrops as $c): ?>
                    <?php if($c['cropid'] == $firstCrop): ?>
                        <option selected="selected" value="<?php echo $c['cropid']?>">
                    <?php else: ?>
                        <option value="<?php echo $c['cropid']?>">
                    <?php endif; ?>
                        <?php echo $c['cropname'] ?>
                        </option>
                <?php endforeach; ?>
                </select>
                
                
                <br />
                <div id="cropData"></div>
                <br />
                
                </p>
                
                
                
                    <object type="application/x-java-applet;version=1.4.1"
                    width= "900" height= "800"  name="jsap" id="jsap">
                        <param name="code" value="croppingplanap.CroppingPlanAp">
                        <param name="mayscript" value="yes">
                        <param name="scriptable" value="true">
                        <?php
                        
                        echo "<param name=\"first_crop\" value=\"$firstCrop\">";
                        echo "<param name=\"all_categories\" value=\"$categoryNameString\">";
                        echo "<param name=\"all_crops\" value=\"$cropNameString\">";
                        echo "<param name=\"map_to_load\" value=\"$planString\">";
                
                        ?>
                        <param name="name" value="jsapplet">
                    </object>
                    
                
            </div><!--col1-->
        </div>
	</div><!-- end #content -->
	
	<div style="clear: both;">&nbsp;</div>
    <!-- end #page -->
    <div id="footer">
        <p>Copyright (c) 2010 Dilmun Hill Team.</p>
    </div>
    <!-- end #footer -->

</body>
</html>